import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from tkinter import Listbox, messagebox
import time

def start_progress():
    """模拟任务进度条"""
    progress_bar["value"] = 0
    root.update_idletasks()
    for i in range(101):
        progress_bar["value"] = i
        time.sleep(0.02)  # 模拟任务
        root.update_idletasks()
    label.config(text="Task Completed!")

def show_selection():
    """显示用户选择的单选、多选和输入框内容"""
    selected_radio = radio_var.get()
    selected_checkboxes = [var.get() for var in check_vars if var.get()]
    input_text = input_entry.get()
    message = (
        f"Radio Selected: {selected_radio}\n"
        f"Checkboxes Selected: {', '.join(selected_checkboxes)}\n"
        f"Input: {input_text}"
    )
    detail_label.config(text=message)

def show_details(event):
    """跳转到列表项详情页"""
    selected_indices = listbox.curselection()
    if selected_indices:
        selected_item = listbox.get(selected_indices[0])
        messagebox.showinfo("Item Details", f"You selected: {selected_item}")

def switch_to_page2():
    """切换到页面2"""
    page1_frame.pack_forget()
    page2_frame.pack(fill="both", expand=True)

def switch_to_page1():
    """切换回页面1"""
    page2_frame.pack_forget()
    page1_frame.pack(fill="both", expand=True)

# 创建主窗口
root = ttk.Window(themename="flatly")
root.title("Enhanced GUI with Page Switching")
root.geometry("400x700")  # 增大窗口高度

# 页面1
page1_frame = ttk.Frame(root)
page1_frame.pack(fill="both", expand=True)

label = ttk.Label(page1_frame, text="Click 'Start' to begin", font=("Arial", 12))
label.pack(pady=10)

progress_bar = ttk.Progressbar(page1_frame, orient="horizontal", length=300, mode="determinate")
progress_bar.pack(pady=10)

start_button = ttk.Button(page1_frame, text="Start Progress", command=start_progress, bootstyle="success")
start_button.pack(pady=10)

radio_var = ttk.StringVar(value="Option 1")
ttk.Label(page1_frame, text="Choose an option:").pack(pady=10)
for option in ["Option 1", "Option 2", "Option 3"]:
    ttk.Radiobutton(page1_frame, text=option, variable=radio_var, value=option).pack(anchor=W)

check_vars = [ttk.StringVar() for _ in range(3)]
ttk.Label(page1_frame, text="Select multiple options:").pack(pady=10)
for var, text in zip(check_vars, ["Choice A", "Choice B", "Choice C"]):
    ttk.Checkbutton(page1_frame, text=text, variable=var, onvalue=text, offvalue="").pack(anchor=W)

ttk.Label(page1_frame, text="Enter text:").pack(pady=10)
input_entry = ttk.Entry(page1_frame, width=30)
input_entry.pack(pady=5)

show_button = ttk.Button(page1_frame, text="Show Selection", command=show_selection, bootstyle="primary")
show_button.pack(pady=10)

detail_label = ttk.Label(page1_frame, text="", font=("Arial", 10))
detail_label.pack(pady=10)

ttk.Label(page1_frame, text="Select an item from the list:").pack(pady=10)
listbox = Listbox(page1_frame, height=5, font=("Arial", 10), exportselection=False)
for item in ["Item 1", "Item 2", "Item 3", "Item 4"]:
    listbox.insert(END, item)
listbox.pack(pady=5)
listbox.bind("<<ListboxSelect>>", show_details)

# 添加切换按钮
switch_button_to_page2 = ttk.Button(page1_frame, text="Go to Page 2", command=switch_to_page2, bootstyle="info")
switch_button_to_page2.pack(pady=20)

# 页面2
page2_frame = ttk.Frame(root)

ttk.Label(page2_frame, text="This is Page 2", font=("Arial", 14)).pack(pady=20)
ttk.Button(page2_frame, text="Go back to Page 1", command=switch_to_page1, bootstyle="primary").pack(pady=10)

# 启动主循环
root.mainloop()
